package com.parfinex.sims.servlet;

import com.parfinex.sims.entity.User;
import com.parfinex.sims.service.UserService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @author lijh
 */
@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        System.out.println("获取到Form表单的用户名是：" + userName + ", 获取到Form表单的密码是：" + password);
//        if (userName.equals(null) || password.equals(null)) {
//            System.out.println("用户名或密码不能为空，请输入正确的用户名或密码");
//            request.setAttribute("error","用户名不能为空，请输入用户名");
//        }
        UserService userService = new UserService();
        User user  = null;
        try {
            user = userService.userLoginService(new User(userName,password));
            System.out.println(user.getUserName()+ " " + user.getPassword());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(user != null || user.equals("")){
            System.out.println("用户" + userName + "登录成功");
            //客户端跳转
            response.sendRedirect("main.jsp");
        } else {
            System.out.println("用户" + userName + "登录失败");
            request.getSession().setAttribute("userName",userName);
            //客户端跳转
            response.sendRedirect("login.jsp");
        }
    }
}
